<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="closeFn">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="confirmFn">确认</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserInfoById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      checkList: [], // 选中的角色列表
      roleList: [] // 角色列表
    }
  },
  async created() {
    // 获取并保存角色列表
    const res = await getRoles({
      page: 1,
      pagesize: 1000
    })
    this.roleList = res.rows
  },
  methods: {
    // 点击确认按钮
    async confirmFn() {
      await assignRoles({
        id: this.userId, // 员工id
        roleIds: this.checkList // 角色id集合
      })
      //   关闭弹窗
      this.closeFn()
    },
    // 关闭弹窗
    closeFn() {
      this.checkList = []
      this.$emit('update:showDialog', false)
    },
    async getRoleIds() {
      // 获取默认员工角色列表数据
      const result = await getUserInfoById(this.userId)
      //   console.log(49, result)
      // 回显角色数据
      this.checkList = result.roleIds
    }
  }
}
</script>

<style>

</style>
